﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Views.Common
{
    public partial class TransferTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetTicketDateList();
                DataBindGirdView();
            }
        }
        private void GetTicketDateList()
        {
            //发车时间
            ListItem item1 = new ListItem("发车时间", "");
            ddlTicketDate.Items.Add(item1);
            for (int i = 0; i < 10; i++)
            {
                ListItem item = new ListItem(DateTime.Now.AddDays(i).ToString("MM月dd日"), DateTime.Now.AddDays(i).ToShortDateString());
                ddlTicketDate.Items.Add(item);
            }
        }

        private void DataBindGirdView()
        {
             
            string sql = "";
            if (Request["from"] != null && Request["to"] != null)
            {
                string strFrom = HttpUtility.UrlDecode(Request["from"].ToString());
                string strTo =HttpUtility.UrlDecode(Request["to"].ToString());
                sql = string.Format(" FromCity = '{0}' AND ToCity ='{1}'  ", strFrom, strTo);
            }
            else if(Request["tonumber"]!=null)
            {
                sql = string.Format(" TicketNumber= '{0}'",HttpUtility.UrlDecode(Request["tonumber"].ToString()));
            }

            if (!string.IsNullOrEmpty(ddlTicketDate.SelectedValue))
            {
                if (sql != "")
                    sql += " AND ";
                sql += string.Format("  TicketDate between '{0}' and '{0}'", ddlTicketDate.SelectedValue);
            }
            if (!string.IsNullOrEmpty(ddlTickeType.SelectedValue))
            {
                if (sql != "")
                    sql += " AND ";
                sql += string.Format(" TicketType = '{0}'", ddlTickeType.SelectedValue);
            }
            if (sql != "")
            {
                sql += " AND [IsDel] = 0 ";

            }
            obsTicketList.SelectParameters["sql"].DefaultValue = sql;
            gdvTicketList.DataBind();
        }

        protected void ddlTickeType_TextChanged(object sender, EventArgs e)
        {
            DataBindGirdView();
        }
        
        protected void ddlTicketDate_TextChanged(object sender, EventArgs e)
        {
            DataBindGirdView();
        }

        public static string GetPastTime(string caption)
        {
           //string caption = "2009-05-05 13:27:20";
            DateTime d1 = DateTime.Parse(caption);

            DateTime d2 = DateTime.Parse(DateTime.Now.ToLongTimeString());

            System.TimeSpan ND = d2 - d1;

            int n = ND.Days;   //天数差

            int hn = ND.Hours; //小时差
            int m = ND.Minutes;// 分钟差

            string res;


            if (n == 0 && hn == 0)
            {
                res = m.ToString() + "分钟前";
            }
            else if (n == 0 && hn > 0)
            {
                res = hn.ToString() + "时" + m.ToString() + "分钟前";
            }
            else
            {
                res = n.ToString() + "天" + hn.ToString() + "时" + m.ToString() + "分钟前";
            }
            return res;
        }
    }
}
